package com.example.ycshareelement.transition;

import android.os.Bundle;
import android.view.View;

import com.google.android.material.imageview.ShapeableImageView;
import com.google.android.material.shape.AbsoluteCornerSize;
import com.google.android.material.shape.CornerSize;
import com.google.android.material.shape.RelativeCornerSize;
import com.google.android.material.shape.ShapeAppearanceModel;

/**
 * Created by huangwei on 2018/10/7.
 */
public class CornerImageViewStateSaver extends ViewStateSaver {

    public static final String TAG_TOP_LEFT = "topLeft";
    public static final String TAG_TOP_RIGHT = "topRight";
    public static final String TAG_BOTTOM_LEFT = "bottomLeft";
    public static final String TAG_BOTTOM_RIGHT = "bottomRight";

    public float getTopLeftSize(Bundle bundle) {
        return bundle.getFloat(TAG_TOP_LEFT);
    }

    public float getTopRightSize(Bundle bundle) {
        return bundle.getFloat(TAG_TOP_RIGHT);
    }

    public float getBottomLeftSize(Bundle bundle) {
        return bundle.getFloat(TAG_BOTTOM_LEFT);
    }

    public float getBottomRightSize(Bundle bundle) {
        return bundle.getFloat(TAG_BOTTOM_RIGHT);
    }

    @Override
    public void captureViewInfo(View view, Bundle bundle) {
        super.captureViewInfo(view, bundle);
        if (view instanceof ShapeableImageView) {
            ShapeableImageView imageView = (ShapeableImageView) view;
            ShapeAppearanceModel shapeAppearanceModel = imageView.getShapeAppearanceModel();
            CornerSize topLeftCornerSize = shapeAppearanceModel.getTopLeftCornerSize();
            CornerSize topRightCornerSize = shapeAppearanceModel.getTopRightCornerSize();
            CornerSize bottomLeftCornerSize = shapeAppearanceModel.getBottomLeftCornerSize();
            CornerSize bottomRightCornerSize = shapeAppearanceModel.getBottomRightCornerSize();

            float topLeftCornerSizeValue = getCornerSizeValue(topLeftCornerSize);
            float topRightCornerSizeValue = getCornerSizeValue(topRightCornerSize);
            float bottomLeftCornerSizeValue = getCornerSizeValue(bottomLeftCornerSize);
            float bottomRightCornerSizeValue = getCornerSizeValue(bottomRightCornerSize);

            bundle.putFloat(TAG_TOP_LEFT, topLeftCornerSizeValue);
            bundle.putFloat(TAG_TOP_RIGHT, topRightCornerSizeValue);
            bundle.putFloat(TAG_BOTTOM_LEFT, bottomLeftCornerSizeValue);
            bundle.putFloat(TAG_BOTTOM_RIGHT, bottomRightCornerSizeValue);
        }
    }

    public static float getCornerSizeValue(CornerSize cornerSize) {
        float cornerSizeValue = 0f;
        if (cornerSize instanceof AbsoluteCornerSize) {
            cornerSizeValue = ((AbsoluteCornerSize) cornerSize).getCornerSize();
        } else if (cornerSize instanceof RelativeCornerSize) {
            cornerSizeValue = ((RelativeCornerSize) cornerSize).getRelativePercent();
        }
        return cornerSizeValue;
    }

}
